﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace chungcu
{
    public partial class nhapdichvu : Form
    {
        private SqlConnection conn;
        private DataTable dt = new DataTable("themdichvu");
        private SqlDataAdapter da = new SqlDataAdapter();
        private Boolean kt;
        private void connect()
        {

            String cn = "Data source=(local);Initial Catalog=Quanlychungcu;Integrated Security=True";
            try
            {
                conn = new SqlConnection(cn);
                conn.Open(); 

            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
                MessageBox.Show("khong thanh cong");
            }
        }

        private void disconnect()
        {
            conn.Close();
            conn.Dispose();
            conn = null;
        }
        public nhapdichvu()
        {
            InitializeComponent();
        }

        private void nhapdichvu_Load(object sender, EventArgs e)
        {
            connect();
            getdata();
        }
        private Boolean kiemtra()
        {
            kt = true;

            if (txtmadv.Text == "")
            {
                MessageBox.Show("Mã dịch vụ không đươc để trống !", "Thông báo", MessageBoxButtons.OK);
                kt = false;
                txtmadv.Focus();
            }
            else if (txttendv.Text == "")
            {
                MessageBox.Show("Tên dịch vụ không được để trống!", "thong bao", MessageBoxButtons.OK);
                kt = false;
                txttendv.Focus();
            }
            return kt;
        }

        private void getdata()
        {
            SqlCommand command = new SqlCommand(); 
            command.Connection = conn; 
            command.CommandType = CommandType.Text;
            command.CommandText = @"select  * from dichvu ";
            da.SelectCommand = command; 
            da.Fill(dt);
            
        }

        private void btnok_Click(object sender, EventArgs e)
        {
            kiemtra();
            if (kt == true)
            {
                DataRow row = dt.NewRow();
                row["dvid"] = Convert.ToInt32(txtmadv.Text);
                row["dvtendichvu"] = txttendv.Text;
                dt.Rows.Add(row);
                SqlCommand commandInsert = new SqlCommand();
                commandInsert.Connection = conn;
                commandInsert.CommandType = CommandType.Text;
                commandInsert.CommandText = @"INSERT dichvu VALUES(@dvid,@dvtendichvu)";
                commandInsert.Parameters.Add("@dvid", SqlDbType.Int, 20, "dvid");
                commandInsert.Parameters.Add("@dvtendichvu", SqlDbType.NVarChar, 50, "dvtendichvu");
                da.InsertCommand = commandInsert;
                da.Update(dt);
                Close();
                Dispose();
                dichvu dv = new dichvu();
                dv.Show();
            }
            
            
            
        }

        private void btncancel_Click(object sender, EventArgs e)
        {
            Close();
            Dispose();
            dichvu dv = new dichvu();
            dv.Show();

        }
    }
}
